<?php
namespace app\common\model;
use think\Model;
use think\Db;

class Mtk{
  
  protected $appkey;
  protected $mtkid;
  protected $token;
  public function __construct(){
	$conf         = Db::name('system')->field('value')->where('name', 'site_config')->cache(true,600)->find();	
	$conf         = unserialize($conf['value']);
    $this->appkey = $conf['app_key'];    //魔题库给接入平台分配的AppKey
	$this->mtkid  = $conf['app_secret']; //魔题库用户唯一标识 UserIndicateId
	$this->token  = $conf['app_token'];
  }
  
  private function sign($data=array()){
    $rd = rand(100000,999999);
	$data['AppKey']   = $this->appkey;
	$data['NonceStr'] = $rd;
	foreach( $data as $key=>$val ) {
	  $pram[strtolower($key)] = $val;
	}
	ksort($pram);
	$string  = $this->dwdata($pram,false);
	$string .= '&key='.$this->token;
	$sign    = strtoupper(md5($string));
	return ['sign'=>$sign,'rd'=>$rd];
  }
  //生成url数据
  private function dwdata($paraMap, $urlencode){
    $buff = "";
    ksort($paraMap);
    foreach ($paraMap as $k => $v){
      if($urlencode) {
        $v = urlencode($v);
      }
      $buff .= strtolower($k) . "=" . $v . "&";
    }
    $reqPar;
	if (strlen($buff) > 0) $reqPar = substr($buff, 0, strlen($buff)-1);
    return $reqPar;
  }
  
  //注册session
  public function userSession( $orgid = 0,$courseid = 0 ){
	if ( !$orgid ) return false;
	$org      = Db::name('organ')->field('org_id,name,parent_id')->where('org_id',$orgid)->find();
	if ( !$org ) return false;
	$name     = $org['name'];
	$name     = mb_substr($name,0,5,'utf-8');
    $sdata    = ['UserCode'=>$org['org_id'].'_'.$courseid,'UserTrueName'=>$name,'UserTypeId'=>1,'CourseId'=>$courseid];
	$res      = $this->mtkexec('UserBind/GetUserSession',$sdata);
	return $res;
  }
  
  public function getExam($teid = ''){
    if ( !$teid ) return false;
	$data = ['TeacherExamId'=>$teid];
	return $this->mtkexec('Exam/GetTeacherExamDetail',$data);
  }
  
  public function ckExam($questionid = ''){
    if ( $questionid == '' ) return false;
	$questionid = explode(',',$questionid);
	$data = ['QuestionIds'=>$questionid];
	return $this->mtkexec('ShanCePing/CheckQuestion_V2',$data);
  }
  
  //延长session
  public function getsessionid(){
    $data = ['UserIndicateId'=>$this->mtkid];
	$res  = $this->mtkexec('UserBind/GetSessionId',$data);
  }
  
  public function getclass($orgid){
    $data['OrganizationCode'] = $orgid;
	return $this->mtkexec('ShanCePing/GetOrganizationCourses',$data); 
  }
  
  private function  mtkexec($uri,$data){
    if ( $uri == '' || $data == '' ) return false;
	$log    = str_replace('/','_',$uri);
	$un     = $uri;
	$url    = Config('MotikuApiHttp').$uri;
	$sign   = $this->sign($data);
	$header = array();
	$data['AppKey']   = $this->appkey;
	$data['Sign']     = $sign['sign'];
	$data['NonceStr'] = (string)$sign['rd'];
	$header[]         = 'Accept: application/json';
	$header[]         = 'Content-Type: application/json';
	$opts = array(
	    CURLOPT_TIMEOUT        => 1200,
		CURLOPT_RETURNTRANSFER => 1,
		CURLOPT_SSL_VERIFYPEER => false,
		CURLOPT_SSL_VERIFYHOST => false,
		CURLOPT_HTTPHEADER     => $header
	);
	$opts[CURLOPT_URL]        = $url;
	$opts[CURLOPT_POST]       = 1;
	$opts[CURLOPT_POSTFIELDS] = json_encode($data);
	$ch    = curl_init();
	curl_setopt($ch,CURLOPT_ENCODING,'gzip');
	curl_setopt_array($ch, $opts);
	$res   = curl_exec($ch);
	$error = curl_error($ch);
	curl_close($ch);
	$res   = json_decode($res,true);
	//$res['cs'] = $data;
	//writelog($log,$res);
	if ( $un == 'ShanCePing/CheckQuestion_V2' || $un == 'Exam/GetTeacherExamDetail' ) return $res;
	return ($res['ApiResultType'] == 1) ? $res['Value'] : false;
  }

  
}